package com.airnews.ticket.ui.flight;

import java.util.Calendar;

import android.app.DatePickerDialog.OnDateSetListener;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airnews.ticket.R;
import com.airnews.ticket.model.City;
import com.airnews.ticket.ui.BaseActivity;
import com.airnews.ticket.ui.CityList;
import com.airnews.ticket.util.Constants;
import com.airnews.ticket.util.DateUtil;

/**
 * 航班查询(leiy)
 * 
 */
public class FlightQuery extends BaseActivity implements OnClickListener {

	private Button btn_left, btn_right, btn_query;
	private LinearLayout from_city, to_city, departure_date, back_date, 
				spinner_title_air, spinner_title_class;
	private ImageView date_split_line;
	private TextView from_city_text, to_city_text;
	private int fromCityPos = -1, toCityPos = -1;//出发到达城市位置
	private Calendar startCal, endCal;
	private TextView departure_date_text, back_date_text, spinner_title_air_text,spinner_title_class_text;
	private String[] flightTypes, airline_list;//舱位

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.flight_query);
		super.onCreate(savedInstanceState);

		startCal = Calendar.getInstance();
		endCal = Calendar.getInstance();
		
		flightTypes = getResources().getStringArray(R.array.flight_class_type);
		airline_list = getResources().getStringArray(R.array.airline_list);
		
		setupView();
		
		
	}

	private void setupView() {
		btn_left = (Button) findViewById(R.id.btn_left);
		btn_left.setOnClickListener(this);
		btn_right = (Button) findViewById(R.id.btn_right);
		btn_right.setOnClickListener(this);
		
		btn_query = (Button) findViewById(R.id.btn_query);
		btn_query.setOnClickListener(this);

		from_city = (LinearLayout) findViewById(R.id.from_city);
		to_city = (LinearLayout) findViewById(R.id.to_city);
		from_city_text = (TextView) findViewById(R.id.from_city_text);
		to_city_text = (TextView) findViewById(R.id.to_city_text);
		from_city.setOnClickListener(this);
		to_city.setOnClickListener(this);
		
		departure_date = (LinearLayout) findViewById(R.id.departure_date);
		back_date = (LinearLayout) findViewById(R.id.back_date);
		date_split_line = (ImageView) findViewById(R.id.date_split_line);
		departure_date_text = (TextView) findViewById(R.id.departure_date_text);
		back_date_text = (TextView) findViewById(R.id.back_date_text);
		
		departure_date.setOnClickListener(this);
		back_date.setOnClickListener(this);
		
		spinner_title_air = (LinearLayout) findViewById(R.id.spinner_title_air);
		spinner_title_class = (LinearLayout) findViewById(R.id.spinner_title_class);
		spinner_title_air_text = (TextView) findViewById(R.id.spinner_title_air_text);
		spinner_title_class_text = (TextView) findViewById(R.id.spinner_title_class_text);
		spinner_title_air.setOnClickListener(this);
		spinner_title_class.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_left: {
				btn_right.setBackgroundResource(R.drawable.tab_button_bg_right);
				btn_left.setBackgroundResource(R.drawable.tab_button_left_selected);
				departure_date.setBackgroundResource(R.drawable.shape_background_state);
				date_split_line.setVisibility(View.GONE);
				back_date.setVisibility(View.GONE);
				break;
			}
			case R.id.btn_right: {
				btn_left.setBackgroundResource(R.drawable.tab_button_bg_left);
				btn_right.setBackgroundResource(R.drawable.tab_button_right_selected);
				departure_date.setBackgroundResource(R.drawable.shape_background_state_top);
				date_split_line.setVisibility(View.VISIBLE);
				back_date.setVisibility(View.VISIBLE);
				
				break;
			}
			case R.id.from_city: {
				Intent it = new Intent(this, CityList.class);
				it.putExtra("city_pos", fromCityPos);
				startActivityForResult(it, 1);
				break;
			}
			case R.id.to_city: {
				Intent it = new Intent(this, CityList.class);
				it.putExtra("city_pos", toCityPos);
				startActivityForResult(it, 2);
				break;
			}
			case R.id.departure_date: {
				
				showDatePickerDialog(startDateSetListener, startCal.get(Calendar.YEAR), 
						startCal.get(Calendar.MONTH), startCal.get(Calendar.DAY_OF_MONTH));
				break;
			}
			case R.id.back_date: {
				
				showDatePickerDialog(backDateSetListener, endCal.get(Calendar.YEAR), 
						endCal.get(Calendar.MONTH), endCal.get(Calendar.DAY_OF_MONTH));
				break;
			}
			
			case R.id.spinner_title_air: {
				new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_dialog_menu_generic)
				.setTitle(R.string.air_select)
				.setItems(airline_list, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						spinner_title_air_text.setText(airline_list[which]);
					}
				}).show();
				break;
			}

			case R.id.spinner_title_class: {//仓位等级
				
				new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_dialog_menu_generic)
				.setTitle(R.string.class_select)
				.setItems(flightTypes, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						spinner_title_class_text.setText(flightTypes[which]);
					}
				}).show();
				break;
			}
			case R.id.btn_query: {//查询
				
				Intent it = new Intent(this, FlightList.class);
				startActivity(it);
				break;
			}
		}
	}
	
	private OnDateSetListener startDateSetListener = new OnDateSetListener(){
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			startCal.set(year, monthOfYear, dayOfMonth);
			departure_date_text.setText(DateUtil.convertDateToString(startCal.getTime()));
		}
	};
	
	private OnDateSetListener backDateSetListener = new OnDateSetListener(){
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			endCal.set(year, monthOfYear, dayOfMonth);
			back_date_text.setText(DateUtil.convertDateToString(endCal.getTime()));
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(RESULT_CANCELED == resultCode){
			return;
		}
		if(requestCode == 1){
			fromCityPos = data.getIntExtra("city_pos", -1);
			City city = Constants.CITY_LIST.get(fromCityPos);
			from_city_text.setText(city.cityName);
		}else if(requestCode == 2){
			toCityPos = data.getIntExtra("city_pos", -1);
			City city = Constants.CITY_LIST.get(toCityPos);
			to_city_text.setText(city.cityName);
		}
	}
	
	
	
	

}